//
//  NDScrollViewRefreshController.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/19.
//

import SwiftUI
import MJRefresh

// 简化的刷新控制器
class NDScrollViewRefreshController: ObservableObject {
    private weak var scrollView: UIScrollView?
    
    func setupRefreshControl(for scrollView: UIScrollView, onRefresh: @escaping () -> Void) {
        self.scrollView = scrollView
        
        let header = MJRefreshNormalHeader()
        
        header.setTitle("", for: .idle)
        header.setTitle("", for: .pulling)
        header.setTitle("", for: .refreshing)
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = true
        
        header.refreshingBlock = {
            onRefresh()
        }
        scrollView.mj_header = header
    }
    
    func beginRefreshing() {
        scrollView?.mj_header?.beginRefreshing()
    }
    
    func endRefreshing() {
        scrollView?.mj_header?.endRefreshing()
    }
}

// 简化的刷新视图
struct NDRefreshableScrollView<Content: View>: UIViewRepresentable {
    let refreshController: NDScrollViewRefreshController
    let onRefresh: () -> Void
    let content: Content
    
    init(refreshController: NDScrollViewRefreshController,
         onRefresh: @escaping () -> Void,
         @ViewBuilder content: () -> Content) {
        self.refreshController = refreshController
        self.onRefresh = onRefresh
        self.content = content()
    }
    
    func makeUIView(context: Context) -> UIScrollView {
        let scrollView = UIScrollView()
        scrollView.alwaysBounceVertical = true
        scrollView.showsVerticalScrollIndicator = false
        
        refreshController.setupRefreshControl(for: scrollView) {
            self.onRefresh()
        }
        
        let hostingController = UIHostingController(rootView: content)
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.view.backgroundColor = .clear
        
        scrollView.addSubview(hostingController.view)
        
        NSLayoutConstraint.activate([
            hostingController.view.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
            hostingController.view.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
            hostingController.view.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
            hostingController.view.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
            hostingController.view.heightAnchor.constraint(greaterThanOrEqualTo: scrollView.frameLayoutGuide.heightAnchor)
        ])
        
        context.coordinator.hostingController = hostingController
        context.coordinator.scrollView = scrollView
        
        return scrollView
    }
    
    func updateUIView(_ uiView: UIScrollView, context: Context) {
        context.coordinator.hostingController?.rootView = content
        
        DispatchQueue.main.async {
            if let hostingView = context.coordinator.hostingController?.view {
                let contentSize = hostingView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
                hostingView.frame.size = contentSize
                uiView.contentSize = contentSize
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    class Coordinator {
        var hostingController: UIHostingController<Content>?
        var scrollView: UIScrollView?
    }
}
